{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python实战-32个常用入门"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 9.输出某个路径及其子目录下所有以.html为后缀的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_dir(filepath):\n",
    "  for i in os.listdir(filepath):\n",
    "    path = os.path.join(filepath, i)\n",
    "    if os.path.isdir(path):\n",
    "      print_dir(path)\n",
    "    if path.endswith(\".html\"):\n",
    "      print(path)\n",
    "\n",
    "filepath = \"\"C:\\Program Files\\JetBrains\\PyCharm 2020.1\"\n",
    "print_dir(filepath)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.把原字典的键值对颠倒并生产新的字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'taobao': 'TAOBAO', 'baidu': 'BAIDU', 'caocao': 'CAOCAO'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1 = {\"TAOBAO\":\"taobao\",\"BAIDU\":\"baidu\",\"CAOCAO\":\"caocao\"}\n",
    "dict2 = {y:x for x,y in dict1.items()}\n",
    "dict2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 11.打印九九乘法表!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 1 = 1 \t\n",
      "\n",
      "1 x 2 = 2 \t\n",
      "2 x 2 = 4 \t\n",
      "\n",
      "1 x 3 = 3 \t\n",
      "2 x 3 = 6 \t\n",
      "3 x 3 = 9 \t\n",
      "\n",
      "1 x 4 = 4 \t\n",
      "2 x 4 = 8 \t\n",
      "3 x 4 = 12 \t\n",
      "4 x 4 = 16 \t\n",
      "\n",
      "1 x 5 = 5 \t\n",
      "2 x 5 = 10 \t\n",
      "3 x 5 = 15 \t\n",
      "4 x 5 = 20 \t\n",
      "5 x 5 = 25 \t\n",
      "\n",
      "1 x 6 = 6 \t\n",
      "2 x 6 = 12 \t\n",
      "3 x 6 = 18 \t\n",
      "4 x 6 = 24 \t\n",
      "5 x 6 = 30 \t\n",
      "6 x 6 = 36 \t\n",
      "\n",
      "1 x 7 = 7 \t\n",
      "2 x 7 = 14 \t\n",
      "3 x 7 = 21 \t\n",
      "4 x 7 = 28 \t\n",
      "5 x 7 = 35 \t\n",
      "6 x 7 = 42 \t\n",
      "7 x 7 = 49 \t\n",
      "\n",
      "1 x 8 = 8 \t\n",
      "2 x 8 = 16 \t\n",
      "3 x 8 = 24 \t\n",
      "4 x 8 = 32 \t\n",
      "5 x 8 = 40 \t\n",
      "6 x 8 = 48 \t\n",
      "7 x 8 = 56 \t\n",
      "8 x 8 = 64 \t\n",
      "\n",
      "1 x 9 = 9 \t\n",
      "2 x 9 = 18 \t\n",
      "3 x 9 = 27 \t\n",
      "4 x 9 = 36 \t\n",
      "5 x 9 = 45 \t\n",
      "6 x 9 = 54 \t\n",
      "7 x 9 = 63 \t\n",
      "8 x 9 = 72 \t\n",
      "9 x 9 = 81 \t\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print('%d x %d = %d \\t'%(j,i,i*j))\n",
    "    print()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过指定end参数的值，可以取消在末尾输出回车符，实现不换行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 x 1 = 1 \t\n",
      "1 x 2 = 2 \t2 x 2 = 4 \t\n",
      "1 x 3 = 3 \t2 x 3 = 6 \t3 x 3 = 9 \t\n",
      "1 x 4 = 4 \t2 x 4 = 8 \t3 x 4 = 12 \t4 x 4 = 16 \t\n",
      "1 x 5 = 5 \t2 x 5 = 10 \t3 x 5 = 15 \t4 x 5 = 20 \t5 x 5 = 25 \t\n",
      "1 x 6 = 6 \t2 x 6 = 12 \t3 x 6 = 18 \t4 x 6 = 24 \t5 x 6 = 30 \t6 x 6 = 36 \t\n",
      "1 x 7 = 7 \t2 x 7 = 14 \t3 x 7 = 21 \t4 x 7 = 28 \t5 x 7 = 35 \t6 x 7 = 42 \t7 x 7 = 49 \t\n",
      "1 x 8 = 8 \t2 x 8 = 16 \t3 x 8 = 24 \t4 x 8 = 32 \t5 x 8 = 40 \t6 x 8 = 48 \t7 x 8 = 56 \t8 x 8 = 64 \t\n",
      "1 x 9 = 9 \t2 x 9 = 18 \t3 x 9 = 27 \t4 x 9 = 36 \t5 x 9 = 45 \t6 x 9 = 54 \t7 x 9 = 63 \t8 x 9 = 72 \t9 x 9 = 81 \t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print('%d x %d = %d \\t'%(j,i,i*j),end='')   \n",
    "    print()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 12.替换列表中所有的6为6a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['harden', 'lampard', '6a', 64, 76, 87, 78, 45, '6a', 66, '6a', 87686, 98, 106, 116, '6a']\n"
     ]
    }
   ],
   "source": [
    "num = [\"harden\",\"lampard\",6,64,76,87,78,45,6,66,6,87686,98,106,116,6]\n",
    "num.count(6)\n",
    "num.index(6)\n",
    "for i in range(num.count(6)):      #获取6出现的次数\n",
    "    ele_index = num.index(6)        #获取首次6出现的坐标\n",
    "    num[ele_index]=\"6a\"            #修改6为6a\n",
    "print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 13.打印每个名字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello,James\n",
      "Hello,Meng\n",
      "Hello,Xin\n"
     ]
    }
   ],
   "source": [
    "L = [\"James\",\"Meng\",\"Xin\"]\n",
    "for i in range(len(L)):\n",
    "    print(\"Hello,%s\"%L[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 14.合并去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 3, 8, 4, 9, 5, 6, 5, 6, 10, 17, 11, 2]\n",
      "{2, 3, 4, 5, 6, 8, 9, 10, 11, 17}\n",
      "[2, 3, 4, 5, 6, 8, 9, 10, 11, 17]\n"
     ]
    }
   ],
   "source": [
    "list1 = [2,3,8,4,9,5,6]\n",
    "list2 = [5,6,10,17,11,2]\n",
    "list3 = list1 + list2\n",
    "print(list3)              #不去重只进行两个列表的组合\n",
    "print(set(list3))         #去重，类型为set需要转换成list\n",
    "print(list(set(list3)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 15.随机生成验证码的两种方式(数字字母)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 编一个6位数的验证码，有数字组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'676156'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#编一个6位数的验证码，有数字组成\n",
    "import random\n",
    "li1 = []\n",
    "for i in range(6):\n",
    "    num = random.randrange(0,10)\n",
    "    li1.append(str(num))\n",
    "s = \"\".join(li1)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 编一个6位数的验证码，有数字和字母组成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'76963S'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "li1 = []\n",
    "for i in range(6):\n",
    "    b = random.randrange(0,10)\n",
    "    if b < 5:\n",
    "        num1 = random.randrange(65,91)\n",
    "        li1.append(chr(num1))\n",
    "    else:\n",
    "        num = random.randrange(0,10)\n",
    "        li1.append(str(num))\n",
    "s = \"\".join(li1)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 或者："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'8lZc7D'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random\n",
    "list1=[]\n",
    "for i in range(65,91):\n",
    "    list1.append(chr(i))        #通过for循环遍历asii追加到空列表中\n",
    "for j in range (97,123):\n",
    "    list1.append(chr(j))\n",
    "for k in range(48,58):\n",
    "    list1.append(chr(k))\n",
    "yanzhengma = random.sample(list1,6)\n",
    "yanzhengma                     #获取到的为列表\n",
    "yanzhengma = ''.join(ma)              #将列表转化为字符串\n",
    "yanzhengma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'J9ERXu'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import random,string\n",
    "str1 = \"0123456789\"\n",
    "str2 = string.ascii_letters     # string.ascii_letters 包含所有字母（大写或小写）的字符串\n",
    "str3 = str1+str2\n",
    "yanzhengma1 = random.sample(str3,6)     #多个字符中选取特定数量的字符\n",
    "yanzhengma1 = ''.join(ma1)              #使用join拼接转换为字符串\n",
    "yanzhengma1                            #通过引入string模块和random模块使用现有的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
